﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Extractor.Manager;
using Extractor.Manager.Entities;
using Infrastructure;

namespace RarExtractor
{
    public partial class Form1 : Form
    {
        public Form1 ()
        {
            InitializeComponent();
          
        }

        private void SaveState ()
        {
           
            if (WindowState == FormWindowState.Normal)
            {
                SettingsManager<FormSettings>.Instane.Settings.MainFormLocation = Location;
                SettingsManager<FormSettings>.Instane.Settings.MainFormSize = Size;
            }
            else
            {
                SettingsManager<FormSettings>.Instane.Settings.MainFormLocation = RestoreBounds.Location;
                SettingsManager<FormSettings>.Instane.Settings.MainFormSize = RestoreBounds.Size;
            }
            SettingsManager<FormSettings>.Instane.Settings.MainFormState = WindowState;

            SettingsManager<FormSettings>.Instane.SaveConfigurations();
        }

        private void RestoreState ()
        {
            if (SettingsManager<FormSettings>.Instane.Settings.MainFormSize == new Size( 0, 0 ))
            {
                return; // state has never been saved
            }
            
                StartPosition = FormStartPosition.Manual;
            Location = SettingsManager<FormSettings>.Instane.Settings.MainFormLocation;
            Size = SettingsManager<FormSettings>.Instane.Settings.MainFormSize;
            // I don't like an app to be restored minimized, even if I closed it that way
            WindowState = SettingsManager<FormSettings>.Instane.Settings.MainFormState ==
              FormWindowState.Minimized ? FormWindowState.Normal : SettingsManager<FormSettings>.Instane.Settings.MainFormState;
        }

        private void Form1_Load ( object sender, EventArgs e )
        {
            RestoreState();
        }

        private void Form1_FormClosing ( object sender, FormClosingEventArgs e )
        {
            SaveState();
        }
    }
}
